package com.example.demo;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.classes.Activeties;
import com.example.demo.classes.Sgin;
import com.example.demo.classes.User;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
public class SginController {
    @Autowired
    private SignMapper sginMapper;
    private UserMapper userMapper = new UserController().getUserMapper();
    private ActivetiesMapper activetiesMapper = new ActivetiesController().getActMapper();

    public SginController() {
    }

    @GetMapping("/sgin")//查询全部
    public List<Sgin> getSgin() {
        return sginMapper.selectList(null);
    }

    @GetMapping("/sgin/{uid}&{aid}")//条件查询
    public List<Sgin> getSgin(@PathVariable long aid,@PathVariable long uid) {
        LambdaQueryWrapper<Sgin> lqw = new LambdaQueryWrapper<>();

        lqw.eq(Sgin::getUid, uid).eq(Sgin::getAid, aid);
        return sginMapper.selectList(lqw);
    }

    //-----参加活动-----
    @PostMapping("/sginAct/{uid}&{aid}")
    public String SginAct(@PathVariable long aid,@PathVariable long uid)
    {

        LambdaQueryWrapper<User> lqw1 = new LambdaQueryWrapper<>();
        lqw1.eq(User::getUid,uid);
        User user = userMapper.selectOne(lqw1);//获取用户id
        if(user==null)return "用户不存在";

        LambdaQueryWrapper<Activeties> lqw0 = new LambdaQueryWrapper<>();
        lqw0.eq(Activeties::getAid,aid);
        Activeties act = activetiesMapper.selectOne(lqw0);//获取活动id
        if(act==null)return "活动不存在";

        //获取日期
        LocalDateTime startTime = act.getStarttime();
        LocalDateTime endTime = act.getEndtime();
        LocalDateTime now = LocalDateTime.now();

        LambdaQueryWrapper<Sgin> lqw2 = new LambdaQueryWrapper<>();
        lqw2.eq(Sgin::getUid,uid).eq(Sgin::getAid,aid);
        Sgin sgin = sginMapper.selectOne(lqw2);//获取参与信息
        if(sgin==null)
        {
            if(now.isBefore(startTime)||now.isAfter(endTime))//时间检查
            {
                return "该时间段不可报名该活动!";
            }

            sgin = new Sgin(uid,aid);
            sginMapper.insert(sgin);//不存在则参与
            return "参与成功";
        }
        return "参与信息已存在";
    }

    //-----签到活动-----
    @PutMapping("/sginin/{uid}&{aid}")
    public String sginin(@PathVariable long uid,@PathVariable long aid)
    {
        LambdaQueryWrapper<Sgin> lqw2 = new LambdaQueryWrapper<>();
        lqw2.eq(Sgin::getUid,uid).eq(Sgin::getAid,aid);
        Sgin sgin = sginMapper.selectOne(lqw2);//获取参与信息
        if(sgin==null)
        {
            return "信息有误!";
        }
        if (sgin.getSignTime().isAfter(LocalDateTime.now()))//默认9999年,变量中时间在实际时间后才可签到
        {
            sgin.setSignTime(LocalDateTime.now());//修改获取到的信息然后返回sql修改
            sginMapper.updateById(sgin);//自动补全真的好好用
            return "签到成功!";
        }
        else
        {
            return "已经签到过了";
        }
    }

    //-----评价活动-----
    @PutMapping("/assess/{uid}&{aid}&{ase}")
    public String assess(@PathVariable long uid,@PathVariable long aid,@PathVariable String ase)
    {
        //String imageFilePath = "";
        if(ase.isEmpty()||ase.length()>=10)
        {
            return "评价内容有误!";
        }

        LambdaQueryWrapper<Sgin> lqw2 = new LambdaQueryWrapper<>();
        lqw2.eq(Sgin::getUid,uid).eq(Sgin::getAid,aid);
        Sgin sgin = sginMapper.selectOne(lqw2);//获取参与信息
        if(sgin==null)
        {
            return "信息有误!";
        }

        sgin.setAssess(ase);
        sginMapper.updateById(sgin);

        return "评价成功!";
    }
}
